home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / IFF2PBM.ZIP / ACKIFF.C next >
C/C++ Source or Header  |  1994-01-10  |  5KB  |  191 lines

  1. /******************* ( Animation Construction Kit 3D ) ***********************/
  2. /*            Deluxe Paint file reader                 */
  3. /* CopyRight (c) 1993  Authors: Jaimi McEntire, Lary Myers             */
  4. /*****************************************************************************/
  5. //
  6. // This function will return a pointer to a buffer that holds the raw image.
  7. // just free the pointer to delete this buffer. After returning, the array
  8. // colordat will hold the adjusted palette of this pic.
  9. //
  10. // Also, this has been modified to only read in form PBM brushes. form ILBM's
  11. // (the "old" type) are not supported. use the "new" deluxe paint .lbm type
  12. // and do not choose "old".
  13.  
  14. #include <stdio.h>
  15. #include <conio.h>
  16. #include <process.h>
  17. #include <bios.h>
  18. #include <fcntl.h>
  19. #include <malloc.h>
  20. #include <mem.h>
  21. #include "ack3d.h"
  22. #include "ackeng.h"
  23. #include "ackext.h"
  24. #include "ackiff.h"
  25.  
  26.  
  27. unsigned char far  colordat[768];   /* maximum it can be...256 colors    */
  28.  
  29. unsigned char far  cplanes[8][80];  /* setting max at 640 pixels width   */
  30.                                     /* thats 8 pixels per byte per plane */
  31. unsigned char far  *pplanes= (char far *) &cplanes[0][0];  /* for a form pbm */
  32.  
  33. int ErrorCode = 0;
  34.  
  35. unsigned char far * AckReadIff(char *picname)
  36.    {
  37.    FILE *pic;
  38.    form_chunk    fchunk;
  39.    ChunkHeader    chunk;
  40.    BitMapHeader bmhd;
  41.    long length;
  42.    char value;       // must remain signed, no matter what. ignore any warnings.
  43.    int sofar;
  44.    int width,height,planes;
  45.    int pixw;
  46.    unsigned char far *destx, *savedestx;
  47.  
  48.    if ((pic = fopen(picname,"r+b"))== NULL)
  49.     {
  50.     ErrorCode = ERR_BADPICNAME;
  51.     return(0L);
  52.     }
  53.  
  54.    fread(&fchunk,1,sizeof(form_chunk),pic); /* read in the first 12 bytes*/
  55.  
  56.    if (fchunk.type != FORM)
  57.       {
  58.       fclose(pic);
  59.       ErrorCode = ERR_INVALIDFORM;
  60.       return(0L);
  61.       }
  62.  
  63.    if (fchunk.subtype != ID_PBM)
  64.       {
  65.       printf("Error: Not form PBM!");
  66.       fclose(pic);
  67.       ErrorCode = ERR_NOPBM;
  68.       return(0L);
  69.       }
  70.    // now lets loop...Because the Chunks can be in any order!
  71.    while(1)
  72.       {
  73.       fread(&chunk,1,sizeof(ChunkHeader),pic);
  74.       ByteFlipLong(&(long)(chunk.ckSize));
  75.       if (chunk.ckSize & 1) chunk.ckSize ++;    // must be word aligned
  76.       if(chunk.ckID == ID_BMHD)
  77.       {
  78.       fread(&bmhd,1,sizeof(BitMapHeader),pic);
  79.       bmhd.w=swab(bmhd.w);            // the only things we need.
  80.       bmhd.h=swab(bmhd.h);
  81.       destx = (unsigned char far *)malloc((bmhd.w * bmhd.h)+4);
  82.       if ( !destx )
  83.         {
  84.         fclose(pic);
  85.         ErrorCode = ERR_NOMEMORY;
  86.         return(0L);
  87.         }
  88.  
  89.       savedestx = destx;
  90.  
  91.       destx[0] = bmhd.w%256;
  92.       destx[1] = bmhd.w/256;
  93.       destx[2] = bmhd.h%256;
  94.       destx[3] = bmhd.h/256;
  95.       destx += 4;
  96.       continue;
  97.       }
  98.       if(chunk.ckID == ID_CMAP)
  99.       {
  100.       int i;
  101.       unsigned char r,g;
  102.  
  103.       fread(colordat,1,chunk.ckSize,pic);
  104.       for (i=0;i<768;i++)
  105.           {
  106.            r = colordat[i];      // r,g do not stand for red and green
  107.           g = r >> 2;
  108.           colordat[i] = g;
  109.           }
  110.       continue;
  111.       }
  112.       if(chunk.ckID == ID_BODY)
  113.       {
  114.       for(height = 0; height<bmhd.h; height ++)
  115.          {
  116.          unsigned char *dest;
  117.          dest = (unsigned char *)&(pplanes[0]); /* point at first char  */
  118.          sofar = bmhd.w;                /* number of bytes = 8  */
  119.          if (sofar&1) sofar++;
  120.          while (sofar)
  121.         {
  122.         if (bmhd.compression)
  123.            {
  124.            value=fgetc(pic);      /* get the next byte      */
  125.            // if (value == 128) continue; /* NOP..just get another*/
  126.            if (value > 0)
  127.               {
  128.               int len;
  129.               len = value +1;
  130.               sofar -= len;
  131.               if(!(fread(dest,len,1,pic)))
  132.               {
  133.               fclose(pic);
  134.               ErrorCode = ERR_BADPICFILE;
  135.               free(savedestx);
  136.               return(0L);
  137.               }
  138.               dest +=len;
  139.               }
  140.            else
  141.               {
  142.               int count;
  143.               count = -value; /* get amount to dup */
  144.               count ++;
  145.               sofar -= count;
  146.               value=fgetc(pic);
  147.               while (--count >= 0) *dest++ = value;
  148.               }
  149.            }
  150.         else
  151.            {
  152.            fread(dest,sofar,1,pic); /* just throw on plane */
  153.            sofar = 0;
  154.            }
  155.         }
  156.          if (sofar < 0)
  157.         {
  158.         fclose(pic);
  159.         }
  160.          _fmemcpy(destx,pplanes,bmhd.w);
  161.          destx += bmhd.w;
  162.          }
  163.       break; /* leave if we've unpacked the BODY*/
  164.       }
  165.       fseek(pic,chunk.ckSize,SEEK_CUR);
  166.       }
  167.  
  168.    fclose(pic);
  169.    return((char far *)savedestx);
  170.    }
  171.  
  172. void ByteFlipLong(long *NUMBER)
  173.    {
  174.    // Hey, I didn;t write this function!!!
  175.    long int Y, T;
  176.    int I;
  177.  
  178.    T = *NUMBER;
  179.    Y=0;for (I=0;I<4;I++){Y = Y | (T & 0xFF);if (I<3) {Y = Y << 8;T = T >> 8;}}
  180.    *NUMBER = Y;
  181.    }
  182.  
  183. int swab(unsigned short number)
  184.    {
  185.    unsigned int xx1,xx2;
  186.    unsigned int result;
  187.  
  188.    xx1 = number <<8; xx2 = number >>8; result = xx1|xx2;
  189.    return(result);
  190.    }
  191.